import { defineStore } from 'pinia'
import { getLoginAPI } from '@/api';
import { ElMessage } from "element-plus"
import type { IaccountType } from "@/views/Login/types"
import Storage from '@/utils/Storeage';
import router from '@/router';
import { LOGIN_TOKEN, USER_MENUS } from '@/global/constants';
import { mapMenuRouter } from '@/utils/mapMenus';

interface ILoginState {
  token: string;
  userInfo: any,
  menus: any
}

const useLoginStore = defineStore('login', {
  // 如何指定state的类型
  state: (): ILoginState => ({
    token: Storage.get(LOGIN_TOKEN) ?? '',
    userInfo: {},
    menus: Storage.get(USER_MENUS) ?? []
  }),
  actions: {
    async getLoginAction(account: IaccountType) {
      // 1.根据账号密码登录 获取 id
      await getLoginAPI(account)
     
      // 3.提示用户
      ElMessage({
        message: `欢迎回来~`,
        type: 'success'
      });

      mapMenuRouter()
      router.push('/home')
    },
    async getRegisterAction() {
      mapMenuRouter()
    }
  },
  getters: {

  }
})

export default useLoginStore
